<template>
	<!-- 退货地址 -->
	<view class="content">
		<view class="row b-b">
			<text class="tit"><text style="color: #8405dc;">*</text>退货地址名称</text>
			<input class="input" type="text" v-model="addressInfo.title" placeholder="退货地址名称" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit"><text style="color: #8405dc;">*</text>联系人</text>
			<input class="input" type="text" v-model="addressInfo.name" placeholder="联系人" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit"><text style="color: #8405dc;">*</text>手机号</text>
			<input class="input" type="number" v-model="addressInfo.mobile" placeholder="联系人手机号码" placeholder-class="placeholder" />
		</view>
		
		<picker @change="bindPickerChange" :value="provinceindex" :range="provinces" :range-key="'areaname'">
			<view class="row b-b">
				<text class="tit"><text style="color: #8405dc;">*</text>省</text>
				<view class="uni-list-cell-db" v-if="provinces.length>0">
					<view class="uni-input">{{addressInfo.province}}</view>
				</view>
			</view>
		</picker>
		<picker @change="bindPickerChange2" :value="cityindex" :range="city" :range-key="'areaname'">
			<view class="row b-b">
				<text class="tit"><text style="color: #8405dc;">*</text>市</text>
				<view class="uni-list-cell-db" >
					<view class="uni-input">{{addressInfo.city}}</view>
				</view>
			</view>
		</picker>
		<picker @change="bindPickerChange3" :value="districtindex" :range="district" :range-key="'areaname'">
			<view class="row b-b">
				<text class="tit"><text style="color: #8405dc;">*</text>县</text>
				<view class="uni-list-cell-db" >
					<view class="uni-input">{{addressInfo.area}}</view>
				</view>
			</view>
		</picker>
		<view class="row b-b">
			<text class="tit"><text style="color: #8405dc;">*</text>详细地址</text>
			<input class="input" type="text" v-model="addressInfo.address" placeholder="详细地址" placeholder-class="placeholder" />
		</view>
		<button class="add-btn" @click="confirm">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressInfo: {
					title: '',
					name:'',
					mobile: '',
					province:"请选择",
					city:"请选择",
					area:"请选择",
					address:"",
				},
				provinces: [],
				provinceindex: 0,
				
				city: [],
				cityindex: 0,
				
				district:[],
				districtindex:0,
			}
		},
		onLoad() {
			this.getprovinces();
		},
		methods: {
			getAddressInfo(){
				this.$axios('info/refundAddress','POST','supplier').then(res=>{
					if(res.data.code==200){
						this.addressInfo=res.data.data;
						this.addressInfo.province=this.addressInfo.province?this.addressInfo.province:'请选择';
						this.addressInfo.city=this.addressInfo.city?this.addressInfo.city:'请选择';
						this.addressInfo.area=this.addressInfo.area?this.addressInfo.area:'请选择';
					}
				})
			},
			getprovinces(){
				this.$axios('getaddress/info', "POST",'shop').then(res => {
					if (res.data.code == 200) {
						this.provinces = res.data.data.data;
						this.getAddressInfo();
					}
				})
			},
			// 省
			bindPickerChange(e) {
				this.provinceindex=e.target.value;
				this.addressInfo.province=this.provinces[this.provinceindex].areaname;
				this.$axios('getaddress/info', "POST",'shop', {
					type:"city",
					parentid:this.provinces[this.provinceindex].id
				}).then(res => {
					if (res.data.code == 200) {
						this.city = res.data.data.data;
						this.addressInfo.city='请选择';
						this.addressInfo.area='请选择';
					}
				})
			},
			// 市
			bindPickerChange2(e) {
				if(this.city.length<=0) return ;
				this.cityindex=e.target.value;
				this.addressInfo.city=this.city[this.cityindex].areaname;
				this.$axios('getaddress/info', "POST",'shop', {
					type:"district",
					parentid:this.city[this.cityindex].id
				}).then(res => {
					if (res.data.code == 200) {
						this.district = res.data.data.data;
						this.addressInfo.area='请选择';
					}
				})
			},
			// 县
			bindPickerChange3(e) {
				if(this.district.length<=0) return ;
				this.districtindex=e.target.value;
				this.addressInfo.area=this.district[this.districtindex].areaname;
			},
			//提交
			confirm() {
				let data = JSON.parse(JSON.stringify(this.addressInfo));
				if (data.title=='') {
					this.$api.msg('请填写退货地址名称！');
					return;
				}
				if (data.name=='') {
					this.$api.msg('请填联系人！');
					return;
				}
				if (!/(^1[2|3|4|5|6|7|8|9][0-9]{9}$)/.test(data.mobile)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (data.province=="请选择"||data.city=="请选择"||data.area=="请选择"||data.address=='') {
					this.$api.msg('请完善地址信息');
					return;
				}
				this.$axios('info/refundAddressSave','POST','supplier',data).then(res=>{
					if(res.data.code==200){
						this.$api.msg('提交成功!')
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16upx;
		color: $font-color-dark;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 170upx;
		}

		.input {
			flex: 1;
			font-size: $font-sm;
			color: $font-color-dark;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
